package demo1.servlet.userBuy;

import com.alibaba.fastjson.JSON;
import demo1.pojo.goods;
import demo1.pojo.users;
import demo1.service.goodsService;
import demo1.service.userService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.BufferedReader;
import java.io.IOException;

@WebServlet("/signIn")
public class signIn extends HttpServlet {//签到
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取session对象
        HttpSession session = req.getSession();
        Object userObject = session.getAttribute("users");
        users users = (users) userObject;

        userService userService = new userService();
        int num = 0;
        try {
            num = userService.signIn(users);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        //调用service验证
        if (num == 1) {
            //签到成功
            resp.getWriter().write("success");
        }
        else if(num == 0){
            //重复签到
            resp.getWriter().write("error");
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
